﻿using Microsoft.Extensions.Logging;
using System;

namespace WebApi.Model
{
    /// <summary>
    /// 接口返回值
    /// </summary>
    public class ReturnModel
    {
        public int Code { get; set; } = 200;

        public int Count { get; set; } = 0;

        public string Message { get; set; }

        public object Data { get; set; } = null;
    }
    /// <summary>
    /// 指定Data类型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ReturnModel<T>
    {
        public int Code { get; set; } = 200;

        public int Count { get; set; } = 0;

        public string Message { get; set; }

        public T Data { get; set; }
    }
    /// <summary>
    /// 扩展帮助类
    /// </summary>
    public static class ExtendHelper
    {
        /// <summary>
        /// 返回202
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ReturnModel Code_202(this ILogger logger, string message)
        {
            logger.LogInformation(message);
            return new ReturnModel { Code = 202, Message = message };
        }

        /// <summary>
        /// 返回错误Model
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static ReturnModel ErrorModel(this ILogger logger, string message, Exception ex)
        {
            logger.LogError(ex, message);
            return new ReturnModel { Code = 500, Message = "出现未知错误" };
        }

        /// <summary>
        /// 返回错误Model
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static ReturnModel<T> ErrorListModel<T>(this ILogger logger, string message, Exception ex)
        {
            logger.LogError(ex, message);
            return new ReturnModel<T> { Code = 500, Message = ex.Message, Data = default };
        }
    }
}
